package com.java.mybatis.controller;

import com.java.mybatis.entity.*;
import com.java.mybatis.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * (Goods)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/goods/")
public class GoodsController {
    /**
     * 服务对象
     */
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private LocationService locationService;
    @Autowired
    private OutboundService outboundService;
    @Autowired
    private OutboundDetailsServices outboundDetailsServices;
    @Autowired
    private GoodstypeService goodstypeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Goods selectOne(Integer id) {
        return this.goodsService.queryById(id);
    }

    /**
     * 拣货任务信息
     * @param model
     * @param page
     * @state state  用户判断是拣货管理还是拣货任务   1拣货任务   2拣货管理
     * @return
     */
    @RequestMapping("queryStateLimit")
    public String queryStateLimit(HttpServletRequest request,Model model, Integer page,Integer state){
        List<Goods> goods = goodsService.queryStateLimit(state, (page-1)*7, 7);
        //分页查询的货物信息
        model.addAttribute("goods",goods);
        //分页当前页
        request.getSession().setAttribute("thisPage",page);
        //根据货物状态货物总条数
        Integer count=goodsService.goodStateCount(state);
        //总共有多少页   7     15  1
        int goodPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
        model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
        //存入总共有多少页
        model.addAttribute("goodPageMax",goodPageMax);
        //存入总共多少条数据
        model.addAttribute("count",count);
        //存入下拉列表数据
        Goodstype goodstype = new Goodstype();
        goodstype.setGoodstypeCode(1);
        List<Goodstype> goodstypeList = goodstypeService.queryAll(goodstype);
        model.addAttribute("goodsTypeList",goodstypeList);
        //根据state返回页面
        return state==1?"System/sys-data":"System/sys-link";
    }

    /**
     * 下拉框查询
     * @param state
     * @param str  输入的类容
     * @return
     */
    @RequestMapping("queryStateSelect")
    public String queryStateSelect(HttpServletRequest request,Model model,Integer state,String str,Integer page){
        if (str!=null&&str!="") {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("state",state);
            hashMap.put("goodsType",str);
            hashMap.put("page",(page-1)*7);
            hashMap.put("pageSize",7);
            request.getSession().setAttribute("selectLikeName",str);
            List<Goods> goods = goodsService.queryStateSelect(hashMap);
            //查询的货物信息
            model.addAttribute("goods",goods);
            //总数据条数
            int count = goodsService.queryStateSelectCount(hashMap);
            //存入总共多少条数据
            model.addAttribute("count",count);
            //存入下拉列表数据
            Goodstype goodstype = new Goodstype();
            goodstype.setGoodstypeCode(1);
            List<Goodstype> goodstypeList = goodstypeService.queryAll(goodstype);
            //死数据
            model.addAttribute("goodsTypeList",goodstypeList);
            //总共有多少页
            int goodPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
            model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
            //存入总共有多少页
            model.addAttribute("goodPageMax",goodPageMax);
            //分页当前页
            request.getSession().setAttribute("thisPage",page);
            //根据state返回页面
            return state==1?"System/sys-data":"System/sys-link";
        }else {
            return "redirect:/goods/queryStateLimit?page="+request.getSession().getAttribute("thisPage")+"&state="+state;
        }
    }

    /**
     * 模糊查询
     * @param request
     * @param model
     * @param state  返回页面也判断
     * @param state1 查询条件判断
     * @param str   查询条件
     * @return
     */
    @RequestMapping("queryStateLike")
    public String queryStateLike(HttpServletRequest request,Model model,Integer state,Integer state1,String str,Integer page){
        if (str!=null&&str!="") {
//            request.getSession().setAttribute("goodsState",state);
//            request.getSession().setAttribute("goodsState1",state1);
            request.getSession().setAttribute("queryLikeName",str);
            HashMap<String, Object> hashMap = new HashMap<>();
            List<Goods> goods = null;
            hashMap.put("state",state);
            hashMap.put("page",(page-1)*7);
            hashMap.put("pageSize",7);
            //总数据条数
            int count =0;
            if (state1>0) {  //根据编号查询
                hashMap.put("goodsOrders","%"+str+"%");
                goods=goodsService.queryStateLikeGoodsOrders(hashMap);
                count=goodsService.queryStateLikeGoodsOrdersCount(hashMap);
            }else{  //根据货物名称查询
                hashMap.put("goodsName","%"+str+"%");
                goods=goodsService.queryStateLikeGoodsName(hashMap);
                count=goodsService.queryStateLikeGoodsNameCount(hashMap);
            }
            //查询的货物信息
            model.addAttribute("goods",goods);
            //存入总共多少条数据
            model.addAttribute("count",count);
            //存入下拉列表数据
            Goodstype goodstype = new Goodstype();
            goodstype.setGoodstypeCode(1);
            List<Goodstype> goodstypeList = goodstypeService.queryAll(goodstype);
            //死数据
            model.addAttribute("goodsTypeList",goodstypeList);
            //总共有多少页
            int goodPageMax = count % 7 == 0 ? count / 7 : count / 7 + 1;
            model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
            //存入总共有多少页
            model.addAttribute("goodPageMax",goodPageMax);
            //分页当前页
            request.getSession().setAttribute("thisPage",page);
            //根据state返回页面
            return state==1?"System/sys-data":"System/sys-link";
        }else {
            return "redirect:/goods/queryStateLimit?page="+request.getSession().getAttribute("thisPage")+"&state="+state;
        }
    }

    /**
     * 修改货物状态
     * @param goodId   货物id
     * @param state   用户判断是拣货管理还是拣货任务   1拣货任务   2拣货管理   修改状态时加1
     * @param address 用于产生出库批号
     * @return
     */
    @RequestMapping("updateState")
    public String updateState(HttpServletRequest request,Integer goodId,Integer state,String address){
        aa(request, goodId, state, address);
        System.out.println("-------------------"+state);
        System.out.println("-------------------"+goodId);
        return "redirect:/goods/queryStateLimit?page="+request.getSession().getAttribute("thisPage")+"&state="+state;
    }
    private void aa(HttpServletRequest request,Integer goodId,Integer state,String address){
        //state大于1时则是出库操作
        if (state>1) {
            //根据货物id查询所在库位的信息
            Location location = locationService.queryByGoodId(goodId);
            //出库时 使用location修改库位使用状态
            locationService.updateLocationStateByLocationId(location.getLocationId());
            //调用方法  修改状态和所在仓库位置   所在仓库位置修改为0
            int i=goodsService.updateStateDelivery(state+1, goodId);
            //获取时间信息  用于产生出库批号
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            String dateFormat = simpleDateFormat.format(new Date());
            int i1=1;
            for (int j = 0; j < i1; j++) {
                //订单批号
                String strNumber =dateFormat + "BatchOrder" + i1;
                //根据编号查询总条数
                int count = outboundService.queryOutboundNumberSum(strNumber);
                if (count>100) {
                    i1++;
                }else if(count==0){
                    //添加批号
                    Outbound outbound = new Outbound();
                    outbound.setOutboundNumber(strNumber);
                    outbound.setOutboundMark(location.getArea().getRegion().getRegionName());
                    //调用outbound的添加方法
                    outboundService.addOutboundBatch(outbound);

                    //根据批号查询id
                    int outBoundId = outboundService.queryOutboundIdByOutboundNumber(strNumber);
                    System.out.println(outBoundId);
                    OutboundDetails outboundDetails = new OutboundDetails();
                    outboundDetails.setOutboundDetailsCode(1);
                    outboundDetails.setOutboundDetailsMark("备用");

                    Goods goods = new Goods();
                    goods.setGoodsId(goodId);
                    outboundDetails.setGoods(goods);

                    Outbound outbound1 = new Outbound();
                    outbound1.setOutboundId(outBoundId);
                    outboundDetails.setOutbound(outbound1);

                    outboundDetailsServices.addOutBoundDetails(outboundDetails);
                }else{
                    //根据批号查询id
                    int outBoundId = outboundService.queryOutboundIdByOutboundNumber(strNumber);
                    OutboundDetails outboundDetails = new OutboundDetails();
                    outboundDetails.setOutboundDetailsCode(1);
                    outboundDetails.setOutboundDetailsMark("备用");

                    Goods goods = new Goods();
                    goods.setGoodsId(goodId);
                    outboundDetails.setGoods(goods);

                    Outbound outbound = new Outbound();
                    outbound.setOutboundId(outBoundId);
                    outboundDetails.setOutbound(outbound);

                    outboundDetailsServices.addOutBoundDetails(outboundDetails);
                }
            }
            System.out.println(address);
        }else{   //分拣操作
            //调用方法修改状态
            int i = goodsService.updateState(state+1, goodId);
        }
    }

    /**
     * 修改货物状态
     * @param str   货物id字符串
     * @param state   用户判断是拣货管理还是拣货任务   1拣货任务   2拣货管理   修改状态时加1
     * @param address 用于产生出库批号
     * @return
     */
    @RequestMapping("updateStateAll")
    public String updateStateAll(HttpServletRequest request, String str, Integer state, String address){
        String[] split = str.split(",");
        for (String s : split) {
            //goodsService.updateState(state+1, Integer.parseInt(s));
            aa(request,Integer.parseInt(s),state+1,address);
        }
        return "redirect:/goods/queryStateLimit?page="+request.getSession().getAttribute("thisPage")+"&state="+state;
    }

    @RequestMapping("selAllGoods")
    public String  selAllGoods(Model model,Integer page,HttpSession session){
        try {
            session.removeAttribute("GoodsNameLike");
            session.removeAttribute("AreaNameLike");
            session.removeAttribute("UserName");
            List<Goods> goods = goodsService.selGoodsAll((page - 1) * 9, 9);
            model.addAttribute("allgoods",goods);
            //查询所有货物有哪些类别
            List<Goods> goods1 = goodsService.selGoodsTyep();
            model.addAttribute("goodsType",goods1);
            //当前页码
            model.addAttribute("thisGoodsPage",page);
            //查询总行数
            Integer count = goodsService.selGoodsCount();
            model.addAttribute("GoodsCount",count);
            //总共有多少页
            int goodPageMax = count % 9 == 0 ? count / 9 : count / 9 + 1;
            model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
            //存入总共有多少页
            model.addAttribute("GoodsPageMax",goodPageMax);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/member-del";
    }


    /**
     * 模糊查询库存管理
     */
    @RequestMapping("selectKcLike")
    public String selectKcLike(String goodsName,Model model)
    {
        if(goodsName.length()==0) {
            System.out.println("----------------------"+goodsName);
            return "redirect:/goods/selectPage?page=1";
        }
        else
        {
            model.addAttribute("goodsName",goodsName);
            List<Goods> goodsList = null;
            try {
                goodsList = goodsService.querySelKcLike("%"+goodsName+"%");
                /*System.out.println(goodsList1);*/
                int size = goodsList.size();
                model.addAttribute("sizeCount1",size);
                /*System.out.println("----------------------"+goodsList);*/
                model.addAttribute("goods1",goodsList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "System/question-del";
    }

    /**
     * 模糊查询盘点管理
     */
    @RequestMapping("querySelInfo")
    public String querySelInfo(Model model,String goodsName) {
        System.out.println(goodsName);
        if(goodsName.length()==0) {
            return "redirect:/goods/querySelPageInfo?page=1";
        }
        else
        {
            List<Goods> goodsList1 = null;
            try {
                goodsList1 = goodsService.querySelInfo("%"+goodsName+"%");
                /*System.out.println(goodsList1);*/
                int size = goodsList1.size();
                model.addAttribute("sizeCount",size);
                /*System.out.println("----------------------"+goodsList1);*/
                model.addAttribute("goods",goodsList1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "System/member-view";
    }



    /**
     * 分页查询盘点管理
     */
    @RequestMapping("querySelPageInfo")
    public String querySelPageInfo(Model model,Integer page) {
        try {
            List<Goods> goods = goodsService.querySelPageInfo((page-1)*7,7);
            int i = goodsService.queryCount();//查询总行数
            model.addAttribute("sizeCount",i);
            //总共有多少页
            int goodTotal = i % 7 == 0 ? i / 7 : i / 7 + 1;
            model.addAttribute("goods",goods);
            model.addAttribute("page2",page);//当前页
            model.addAttribute("pageSize","当前第"+page+"页,共"+goodTotal+"页");
            //存入总共有多少页
            model.addAttribute("CountTotal",goodTotal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/member-view";
    }

    /**
     * 分页查询库存管理
     */
    @RequestMapping("selectPage")
    public String selectPage(Integer page,Model model)
    {
        try {
            List<Goods> goods = goodsService.querySelPage((page - 1) * 5, 5);
            int i1 = goodsService.queryNumber();//查询总行数
            model.addAttribute("sizeCount1",i1);
            //总共有多少页
            int goodsTotal = i1 % 5 == 0 ? i1 / 5 : i1 / 5 + 1;
            model.addAttribute("goods1",goods);
            model.addAttribute("page1",page);//当前页
            model.addAttribute("pageSize","当前第"+page+"页,共"+goodsTotal+"页");
            //存入总共有多少页
            model.addAttribute("goodsTotal",goodsTotal);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/question-del";
    }


    @RequestMapping("SelGoodsLike")
    public String SelGoodsLike(Model model, String name, Integer page, HttpSession session){
        try {
            if(name!=null&&name!=""){
                List<Goods> goods = goodsService.selGoodsLike(name, (page - 1) * 3, 3);
                model.addAttribute("allgoods",goods);
                Integer count = goodsService.selGoodsLikeCount(name);
                model.addAttribute("GoodsCount",count);
                //当前页码
                model.addAttribute("thisGoodsPage",page);
                //总共有多少页
                int goodPageMax = count % 3 == 0 ? count / 3 : count / 3 + 1;
                model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
                //存入总共有多少页
                model.addAttribute("GoodsPageMax",goodPageMax);
                session.setAttribute("GoodsNameLike",name);
            }else {
                return "redirect:/goods/selAllGoods?page=1";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "System/member-del";
    }

    @RequestMapping("selGoodsAreaLike")
    public String selGoodsAreaLike(Model model,String area,Integer page,HttpSession session) throws Exception {
        if(area!=null&&area!=""){
            List<Goods> goods = goodsService.selGoodsArea(area, (page - 1) * 3, 3);
            model.addAttribute("allgoods",goods);
            int count = goodsService.selGoodsAreaCount(area);
            model.addAttribute("GoodsCount",count);
            //当前页码
            model.addAttribute("thisGoodsPage",page);
            //总共有多少页
            int goodPageMax = count % 3 == 0 ? count / 3 : count / 3 + 1;
            model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
            //存入总共有多少页
            model.addAttribute("GoodsPageMax",goodPageMax);
            session.setAttribute("AreaNameLike",area);
        }else {
            return "redirect:/goods/selAllGoods?page=1";
        }
        return "System/member-del";
    }

    @RequestMapping("selUserNameLike")
    public String selUserNameLike(Model model,String name,Integer page,HttpSession session) throws Exception {
        if(name!=null&&name!="") {
            List<Goods> goods = goodsService.selGoodsUserName(name, (page - 1) * 3, 3);
            model.addAttribute("allgoods",goods);
            int count = goodsService.selGoodsNameCount(name);
            model.addAttribute("GoodsCount",count);
            //当前页码
            model.addAttribute("thisGoodsPage",page);
            //总共有多少页
            int goodPageMax = count % 3 == 0 ? count / 3 : count / 3 + 1;
            model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
            //存入总共有多少页
            model.addAttribute("GoodsPageMax",goodPageMax);
            session.setAttribute("UserName",name);
        }else {
            return "redirect:/goods/selAllGoods?page=1";
        }
        return "System/member-del";
    }

    /**
     * 查询货物名称的方法
     * @param model 所有货物名称
     * @return 对应的页面
     */
    @RequestMapping("queryGoodsName")
    public String queryGoodsName(Model model){
        //调用查询货物名称的方法
        List<Goods> goods = goodsService.queryGoodsName();
        //打印测试
        System.out.println(goods);
        //将获取的货物名称存入model中
        model.addAttribute("goods",goods);
        //返回对应的页面
        return "System/echart-4";
    }
}